import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  }
}

const actions = {
  // 登出方法修改（移除API调用依赖）
  logout({ commit, state }) {
    return new Promise((resolve) => {
      removeToken() // 清除token
      resetRouter() // 重置路由
      commit('RESET_STATE') // 重置状态
      resolve()
    })
  },

  // 清除token（用于前端退出）
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken()
      commit('RESET_STATE')
      resolve()
    })
  }
  

  // 移除原有的login、getInfo、register方法，这些逻辑已移到登录页面
}

export default {
  namespaced: true,
  state,
  mutations,
  actions: {
    getInfo({ commit }) {
      // 实现获取用户信息的逻辑
    }
  }
}